package com.zhou.syt.hosp.controller.api;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import com.zhou.syt.common.exception.YyghException;
import com.zhou.syt.common.result.Result;
import com.zhou.syt.common.result.ResultCodeEnum;
import com.zhou.syt.helper.HttpRequestHelper;
import com.zhou.syt.hosp.service.DepartmentService;
import com.zhou.syt.hosp.service.HospitalService;
import com.zhou.syt.hosp.service.HospitalSetService;
import com.zhou.syt.hosp.service.ScheduleService;
import com.zhou.syt.util.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName: ApiController
 * @Description:
 * @Author: admin
 * @Date: 2022/1/7 10:53
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Resource
    private HospitalService hospitalService;

    @Resource
    private HospitalSetService hospitalSetService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private ScheduleService scheduleService;

    //删除排班
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) stringObjectMap.get("hoscode");
        String hosScheduleId = (String) stringObjectMap.get("hosScheduleId");
        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    //查询排班
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) stringObjectMap.get("hoscode");

        String depcode = (String) stringObjectMap.get("depcode");

        int page = StringUtils.isEmpty(stringObjectMap.get("page")) ? 1 : Integer.parseInt((String) stringObjectMap.get("page"));

        int limit = StringUtils.isEmpty(stringObjectMap.get("limit")) ? 1 : Integer.parseInt((String) stringObjectMap.get("limit"));
        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hospCode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);

        return Result.ok(pageModel);
    }


    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(stringObjectMap);
        return Result.ok();
    }

    //删除科室接口
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) stringObjectMap.get("hoscode");
        String depcode = (String) stringObjectMap.get("depcode");
        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) stringObjectMap.get("hoscode");

        int page = StringUtils.isEmpty(stringObjectMap.get("page")) ? 1 : Integer.parseInt((String) stringObjectMap.get("page"));

        int limit = StringUtils.isEmpty(stringObjectMap.get("limit")) ? 1 : Integer.parseInt((String) stringObjectMap.get("limit"));
        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hospCode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);

        return Result.ok(pageModel);
    }

    //上传科室接口
    @PostMapping("/saveDepartment")
    public Result saveDepartMent(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service进行保存
        departmentService.save(stringObjectMap);

        return Result.ok();

    }


    @PostMapping("/hospital/show")
    @ApiOperation("获取医院信息")
    public Result getHospital(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service获取信息
        Hospital hospital = hospitalService.getByHoscode(hospCode);
        return Result.ok(hospital);
    }

    @PostMapping("saveHospital")
    @ApiOperation("上传医院接口")
    public Result saveHosp(HttpServletRequest request) {
        //获取传输过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        //对map进行格式转换，
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        //获取传输过来的key
        String sign = (String) stringObjectMap.get("sign");
        //根据传递过来的医院编码，获取数据库中相应医院的key
        String hospCode = (String) stringObjectMap.get("hoscode");
        //通过获取医院设置的key和当前传入key进行比对
        String key = hospitalSetService.getKey(hospCode);
        //对获取的hospcode进行md5加密
        String signKey = MD5.encrypt(key.toString());
        //进行比对
        if (!ObjectUtils.nullSafeEquals(sign, signKey)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //解决图片中的+号问题
        String logoData = (String) stringObjectMap.get("logoData");
        String s = logoData.replaceAll(" ", "+");
        stringObjectMap.put("logoData", s);
        //调用service对数据进行存储
        hospitalService.save(stringObjectMap);
        return Result.ok();
    }
}
